<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <div style="width: 100%;font-size: 50px ">
        <input type="input" style="width: 500px;height: 50px;margin-top: 50px;margin-left: 20px" id="code" placeholder="请输入淘宝口令" />
        <br>
        <input type="button" style="height: 50px ;margin-left: 20px;width: 100px"  value="获取视频" onclick="getVideo()">
        <input type="button" style="height: 50px ;margin-left: 20px;width: 100px"  value="清空" onclick="clearCode()">
        <div id="info">

        </div>
        <video id="video" style="display: none;width: 500px;height: 500px" src="" controls="controls">
        </video>
    </div>
</head>
<body>
<script>
    function clearCode() {
        var code = document.getElementById("code");
        code.value = '';
        setInfo("");
        document.getElementById("video").style.display="none";
    }
    function getVideo() {
        setInfo("获取中...");
        var code = document.getElementById("code");
        console.log("code", code.value);
        var codeHttpRequest = new XMLHttpRequest();
        codeHttpRequest.open('GET', 'http://lan1.luobiao.vip:9999'+"\\"+code.value, true);
        //codeHttpRequest.open('GET', 'http://localhost:9999'+"\\"+code.value, true);
        codeHttpRequest.send();
        var videoUrl;
        codeHttpRequest.onreadystatechange = function () {
            try {

                if (codeHttpRequest.readyState == 4 && codeHttpRequest.status == 200) {
                    videoUrl = codeHttpRequest.responseText;
                    console.log(videoUrl);
                    var json = {'product_url':videoUrl};
                    var httpRequest = new XMLHttpRequest();
                    httpRequest.open('POST', 'https://www.dabao.website/app', true);
                    httpRequest.setRequestHeader("Content-type","application/json");
                    httpRequest.send(JSON.stringify(json));
                    console.log(videoUrl);
                    httpRequest.onreadystatechange = function () {
                        if (httpRequest.readyState == 4 && httpRequest.status == 200) {
                            var retJson=JSON.parse(httpRequest.responseText)
                            console.log(retJson);
                            var downloadUrl=retJson['download_url'];
                            if ("" == downloadUrl) {
                                reTry(videoUrl);
                            }else{
                                setInfo("获取成功！");
                                console.log(downloadUrl);
                                document.getElementById("video").setAttribute("src", downloadUrl);
                                document.getElementById("video").style.display="block";
                            }
                        }else if (httpRequest.status == 500 && httpRequest.readyState == 4) {
                            reTry(videoUrl);
                        }
                    };
                }else if(codeHttpRequest.readyState == 4 && codeHttpRequest.status == 500){
                    setInfo("获取失败！");
                }
            }catch (e) {
                setInfo("获取失败！");
            }

        };

        function reTry(videoUrl) {
            setInfo("解析失败了！换个域名重试中...");
            videoUrl = videoUrl.replace("https://item.taobao.com", "https://detail.tmall.com");
            var json = {'product_url':videoUrl};
            var httpRequest = new XMLHttpRequest();
            httpRequest.open('POST', 'https://www.dabao.website/app', true);
            httpRequest.setRequestHeader("Content-type","application/json");
            httpRequest.send(JSON.stringify(json));
            httpRequest.onreadystatechange = function () {
                if (httpRequest.readyState == 4 && httpRequest.status == 200) {
                    var retJson=JSON.parse(httpRequest.responseText)
                    var downloadUrl=retJson['download_url'];
                    if ("" == downloadUrl) {
                        setInfo("获取失败！");
                    }else{
                        setInfo("获取成功！");
                        console.log(downloadUrl);
                        document.getElementById("video").setAttribute("src", downloadUrl);
                        document.getElementById("video").style.display="block";
                    }
                }else if (httpRequest.status == 500 && httpRequest.readyState == 4) {
                    setInfo("获取失败！");
                }
            }
        }

    }

    function setInfo(str) {
        document.getElementById("info").textContent=str;
    }


</script>
</body>
</html>